package cn.ztuo.bitrade.service;

import cn.ztuo.bitrade.dao.WinningRecordDao;
import cn.ztuo.bitrade.entity.QWinningRecord;
import cn.ztuo.bitrade.entity.WinningRecord;
import cn.ztuo.bitrade.service.Base.BaseService;
import cn.ztuo.bitrade.util.LikeUtils;
import cn.ztuo.bitrade.util.PredicateUtils;
import cn.ztuo.bitrade.vo.WinningRecordVO;
import com.querydsl.core.types.Predicate;
import com.querydsl.core.types.dsl.BooleanExpression;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.util.ArrayList;

/**
 * @author: Xiazt
 * @date: 2020-04-23 10:48
 */
@Service
public class WinningRecordService extends BaseService {

    @Autowired
    private WinningRecordDao winningRecordDao;

    public Page<WinningRecord> getByPage(WinningRecordVO winningRecordVO) {
        ArrayList<BooleanExpression> booleanExpressions = new ArrayList<>();
        if (StringUtils.isNotEmpty(winningRecordVO.getUsername())) {
            booleanExpressions.add(QWinningRecord.winningRecord.member.username.like(LikeUtils.like(winningRecordVO.getUsername())));
        }
        if (StringUtils.isNotEmpty(winningRecordVO.getMobilePhone())) {
            booleanExpressions.add(QWinningRecord.winningRecord.member.mobilePhone.like(LikeUtils.like(winningRecordVO.getMobilePhone())));
        }
        if (StringUtils.isNotEmpty(winningRecordVO.getPeriods())) {
            booleanExpressions.add(QWinningRecord.winningRecord.lottery.periods.like(LikeUtils.like(winningRecordVO.getPeriods())));
        }
        if (StringUtils.isNotEmpty(winningRecordVO.getNumber())) {
            booleanExpressions.add(QWinningRecord.winningRecord.lottery.number.like(LikeUtils.like(winningRecordVO.getNumber())));
        }
        if (winningRecordVO.getLevel() != null) {
            booleanExpressions.add(QWinningRecord.winningRecord.awardsSetting.level.eq(winningRecordVO.getLevel()));
        }
        Predicate predicate = PredicateUtils.getPredicate(booleanExpressions);
        Sort sort = new Sort(Sort.Direction.DESC, "createTime");
        Pageable pageable = new PageRequest(winningRecordVO.getPageNum() - 1, winningRecordVO.getPageSize(), sort);
        return winningRecordDao.findAll(predicate, pageable);
    }

}
